package com.xxxy.dongyexuan.service.impl;



import com.xxxy.dongyexuan.service.AuthCodeService;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;



@Service
public class AuthCodeServiceImpl implements AuthCodeService {
    private String findSession;

    /**
     * 绑定用户验证码
     *
     * @param sessionId sessionId
     * @param authCode  验证码文本
     */
    @Override
    public void bandUserAuthCode(String sessionId, String authCode) {
        authCodeMap.put(sessionId, authCode);
        findSession = authCodeMap.get(sessionId);
    }

    /**
     * 验证用户验证码
     *
     * @param sessionId sessionId
     * @param authCode  验证码文本
     */
    @Override
    public Boolean authUserCode(String sessionId, String authCode) {
        System.out.println("authUserCode: " + authCode);
//        String text = authCodeMap.get(sessionId);
        String text = findSession;
        System.out.println(authCodeMap.get(sessionId));
        System.out.println("text: " + text);
        if (authCode.isEmpty() || !authCode.equals(text)) {//验证码为空或不匹配
            return false;
        }
        // 验证码验证通过，删除验证码
        authCodeMap.remove(sessionId);
        return true;
    }

    /**
     * 绘制验证码图片
     *
     * @param text 文本内容
     * @return
     */
    @Override
    public ResponseEntity<byte[]> drawAuthCodeImg(String text) throws IOException {
        //创建一个115*45的画布
        BufferedImage canvas = new BufferedImage(115, 45, BufferedImage.TYPE_INT_RGB);

        //获取画布打画笔对象
        Graphics g2d = canvas.getGraphics();

        //创建颜色数组，用于绘制随机颜色
        ArrayList<Color> colorList = new ArrayList<>();
        colorList.add(Color.cyan);
        colorList.add(Color.PINK);
        colorList.add(Color.ORANGE);
        colorList.add(Color.green);

        //创建随机数对象
        Random rd = new Random();

        for (int i = 0; i < text.length(); i++) {
            //获取随机颜色索引
            int index = rd.nextInt(colorList.size());
            //设置画笔随机颜色
            g2d.setColor(colorList.get(index));
            //设置字体大小
            g2d.setFont(new Font(null, Font.BOLD, 15));

            //计算随机x坐标
            int xPoint = (i + 1) * 20;

            //计算随机y坐标
            int yPoint = rd.nextInt(canvas.getHeight() / 2) + 15;

            g2d.setFont(new Font(null, Font.BOLD, 25));
            //绘制验证码
            g2d.drawString(String.valueOf(text.charAt(i)), xPoint, yPoint);

            //绘制字母
            g2d.drawString(text.charAt(i) + "", xPoint, yPoint);
        }
        //绘制15条干扰线
        for (int i = 0; i < 15; i++) {
            g2d.setColor(colorList.get(rd.nextInt(colorList.size())));
            g2d.drawLine(rd.nextInt(canvas.getWidth()), rd.nextInt(canvas.getHeight()), rd.nextInt(canvas.getWidth()), rd.nextInt(canvas.getHeight()));
        }
        //将图片转换为输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ImageIO.write(canvas, "png", baos);
        //释放画布资源
        g2d.dispose();

        return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(baos.toByteArray());
    }

}
